package com.huanglj.himalaya.data.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.huanglj.himalaya.base.BaseApplication;
import com.huanglj.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.Announcer;

import java.util.ArrayList;
import java.util.List;

import static com.huanglj.himalaya.utils.Constants.SUB_ALBUM_ID;
import static com.huanglj.himalaya.utils.Constants.SUB_AUTHOR_NAME;
import static com.huanglj.himalaya.utils.Constants.SUB_COVER_URL;
import static com.huanglj.himalaya.utils.Constants.SUB_DESCRIPTION;
import static com.huanglj.himalaya.utils.Constants.SUB_PLAY_COUNT;
import static com.huanglj.himalaya.utils.Constants.SUB_TB_NAME;
import static com.huanglj.himalaya.utils.Constants.SUB_TITLE;
import static com.huanglj.himalaya.utils.Constants.SUB_TRACKS_COUNT;

public class SubscriptionDao implements ISubDao {

    private static final String TAG = "SubscriptionDao";
    private static final SubscriptionDao ourInstance = new SubscriptionDao();
    private final XimalayaDBHelper mXimalayaDBHelper;
    private ISubDaoCallback mCallBack = null;

    public static SubscriptionDao getInstance() {
        return ourInstance;
    }

    private SubscriptionDao() {

        mXimalayaDBHelper = new XimalayaDBHelper(BaseApplication.getContext());
    }

    @Override
    public void addAlbum(Album album) {
        SQLiteDatabase db = null;
        boolean isSuccess = false;
        try{
            db = mXimalayaDBHelper.getReadableDatabase();
            db.beginTransaction();
            ContentValues contentValues = new ContentValues();
            contentValues.put(SUB_COVER_URL,album.getCoverUrlLarge());
            contentValues.put(SUB_TITLE,album.getAlbumTitle());
            contentValues.put(SUB_DESCRIPTION,album.getAlbumIntro());
            contentValues.put(SUB_PLAY_COUNT,album.getPlayCount());
            contentValues.put(SUB_TRACKS_COUNT,album.getIncludeTrackCount());
            contentValues.put(SUB_AUTHOR_NAME,album.getAnnouncer().getNickname());
            contentValues.put(SUB_ALBUM_ID,album.getId());
            db.insert(SUB_TB_NAME,null,contentValues);
            db.setTransactionSuccessful();
           isSuccess = true;
        }catch (Exception e){
            isSuccess = false;
            e.printStackTrace();
        }finally {

            db.endTransaction();
            db.close();
            if (mCallBack != null) {
                mCallBack.onAddResult(isSuccess);
            }
        }

    }

    @Override
    public void deleteAlbum(Album album) {
        boolean isSuccess = false;
        SQLiteDatabase db = null;
        try{
            db = mXimalayaDBHelper.getReadableDatabase();
            db.beginTransaction();
            int delete = db.delete(SUB_TB_NAME, SUB_ALBUM_ID + "=?", new String[]{album.getId() + ""});
            LogUtil.d(TAG,delete+"");
            db.setTransactionSuccessful();
            isSuccess = true;
        }catch (Exception e){
            isSuccess = false;
            e.printStackTrace();
        }finally {
            db.endTransaction();
            db.close();
            if (mCallBack != null) {
                mCallBack.onDeleteResult(isSuccess);
            }
        }
    }

    @Override
    public void getAlbumList() {
        SQLiteDatabase db = null;
        boolean isSuccess = false;
        List<Album> result = new ArrayList<>();
        try{
            db = mXimalayaDBHelper.getReadableDatabase();
            db.beginTransaction();
            Cursor query = db.query(SUB_TB_NAME, null, null, null, null, null, "_id desc");
            while (query.moveToNext()){
                Album album = new Album();
                String coverUrl = query.getString(query.getColumnIndex(SUB_COVER_URL));
                album.setCoverUrlLarge(coverUrl);
                String title = query.getString(query.getColumnIndex(SUB_TITLE));
                album.setAlbumTitle(title);
                String description = query.getString(query.getColumnIndex(SUB_DESCRIPTION));
                album.setAlbumIntro(description);
                int playCount = query.getInt(query.getColumnIndex(SUB_PLAY_COUNT));
                album.setPlayCount(playCount);
                int anInt = query.getInt(query.getColumnIndex(SUB_TRACKS_COUNT));
                album.setIncludeTrackCount(anInt);
                String authorName = query.getString(query.getColumnIndex(SUB_AUTHOR_NAME));
                Announcer announcer = new Announcer();
                announcer.setNickname(authorName);
                album.setAnnouncer(announcer);
                int albumId = query.getInt(query.getColumnIndex(SUB_ALBUM_ID));
                album.setId(albumId);
                result.add(album);
            }
            db.setTransactionSuccessful();
            isSuccess = true;
        }catch (Exception e){
            isSuccess = false;
            e.printStackTrace();
        }finally {
            db.endTransaction();
            db.close();
            if (mCallBack != null && isSuccess == true) {
                mCallBack.onGetListResult(result);
            }
        }
    }

    @Override
    public void unRegisterViewCallBack(ISubDaoCallback iSubDaoCallback) {
        mCallBack = null;
    }

    @Override
    public void registerViewCallBack(ISubDaoCallback iSubDaoCallback) {
        this.mCallBack = iSubDaoCallback;
    }
}
